"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : SQL_DDL_Drop_Owned
Case Name   : DROP OWNED只影响当前数据库中的对象
Create At   : 2022/08/24
Owner       : @li-xin12345
Description :
    1.创建用户，创建数据库
    2.在数据库1中创建表1和视图1
    3.在数据库2中创建表2和视图2
    4.在数据库1中删除用户所拥有的对象
    5.删除数据库，删除用户
Expect      :
    1.创建成功
    2.创建成功
    3.创建成功
    4.数据库1中用户所拥有的对象删除成功，数据库2中的对象删除失败
    5.删除成功
History     :
    Modified by @kyeleze 2022/11/17:优化用例，保证\d查询结果匹配
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class DropOwned(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.user_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.user_name = 'u_drop_owned_0008'
        self.db_name_1 = 'db_drop_owned_0008_1'
        self.db_name_2 = 'db_drop_owned_0008_2'
        self.table_name_1 = 't_drop_owned_0008_1'
        self.table_name_2 = 't_drop_owned_0008_2'
        self.view_name_1 = 'v_drop_owned_0008_1'
        self.view_name_2 = 'v_drop_owned_0008_2'

    def test_drop_owned(self):
        text = "-----step1:创建用户，创建数据库;expect:创建成功-----"
        self.logger.info(text)
        create_user = f"drop user if exists {self.user_name};" \
            f"create user {self.user_name} " \
            f"sysadmin password '{macro.COMMON_PASSWD}';" \
            f"drop database if exists {self.db_name_1};" \
            f"drop database if exists {self.db_name_2};" \
            f"create database {self.db_name_1};" \
            f"create database {self.db_name_2};"
        create_msg = self.primary_sh.execut_db_sql(create_user)
        self.logger.info(create_msg)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG,
                      create_msg, '执行失败' + text)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG,
                      create_msg, '执行失败' + text)
        self.assertEquals(2, create_msg.count(
            self.constant.DROP_DATABASE_SUCCESS), '执行失败' + text)
        self.assertEquals(2, create_msg.count(
            self.constant.CREATE_DATABASE_SUCCESS), '执行失败' + text)

        text = "-----step2:在数据库1中创建表1和视图1;expect:创建成功-----"
        self.logger.info(text)
        sql_cmd = f"drop table if exists {self.table_name_1};" \
            f"create table {self.table_name_1}(id int, name varchar(15));" \
            f"drop view if exists {self.view_name_1};" \
            f"create view {self.view_name_1} as " \
            f"select * from {self.table_name_1};"
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_name} -W \'{macro.COMMON_PASSWD}\'',
            dbname=self.db_name_1)
        self.logger.info(sql_msg)
        self.assertNotIn('ERROR', sql_msg, '执行失败' + text)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.DROP_VIEW_SUCCESS_MSG,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.CREATE_VIEW_SUCCESS_MSG,
                      sql_msg, '执行失败' + text)
        sql_msg = self.primary_sh.execut_db_sql(
            '\\d', f'-U {self.user_name} -W \'{macro.COMMON_PASSWD}\'',
            dbname=self.db_name_1)
        self.logger.info(sql_msg)
        self.assertIn(self.table_name_1, sql_msg.splitlines()[-3],
                      '执行失败' + text)
        self.assertIn(self.user_name, sql_msg.splitlines()[-3],
                      '执行失败' + text)
        self.assertIn(self.view_name_1, sql_msg.splitlines()[-2],
                      '执行失败' + text)
        self.assertIn(self.user_name, sql_msg.splitlines()[-2],
                      '执行失败' + text)

        text = "-----step3:在数据库2中创建表2和视图2;expect:创建成功-----"
        self.logger.info(text)
        sql_cmd = f"drop table if exists {self.table_name_2};" \
            f"create table {self.table_name_2}(id int, name varchar(15));" \
            f"drop view if exists {self.view_name_2};" \
            f"create view {self.view_name_2} as " \
            f"select * from {self.table_name_2};"
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_name} -W \'{macro.COMMON_PASSWD}\'',
            dbname=self.db_name_2)
        self.logger.info(sql_msg)
        self.assertNotIn('ERROR', sql_msg, '执行失败' + text)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.DROP_VIEW_SUCCESS_MSG,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.CREATE_VIEW_SUCCESS_MSG,
                      sql_msg, '执行失败' + text)
        sql_msg = self.primary_sh.execut_db_sql(
            '\\d', f'-U {self.user_name} -W \'{macro.COMMON_PASSWD}\'',
            dbname=self.db_name_2)
        self.logger.info(sql_msg)
        self.assertIn(self.table_name_2, sql_msg.splitlines()[-3],
                      '执行失败' + text)
        self.assertIn(self.user_name, sql_msg.splitlines()[-3],
                      '执行失败' + text)
        self.assertIn(self.view_name_2, sql_msg.splitlines()[-2],
                      '执行失败' + text)
        self.assertIn(self.user_name, sql_msg.splitlines()[-2],
                      '执行失败' + text)

        text = "-----step4:在数据库1中删除用户所拥有的对象;expect:" \
               "数据库1中用户所拥有的对象删除成功，数据库2中的对象删除失败-----"
        self.logger.info(text)
        sql_cmd = f"drop owned by {self.user_name} cascade;\\d"
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_name} -W \'{macro.COMMON_PASSWD}\'',
            dbname=self.db_name_1)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.DROP_OWNED_SUCCESS, sql_msg, '执行失败' + text)
        self.assertIn('No relations found', sql_msg, '执行失败' + text)
        sql_msg = self.primary_sh.execut_db_sql(
            '\\d', f'-U {self.user_name} -W \'{macro.COMMON_PASSWD}\'',
            dbname=self.db_name_2)
        self.logger.info(sql_msg)
        self.assertNotIn('ERROR', sql_msg, '执行失败' + text)
        self.assertIn(self.table_name_2, sql_msg.splitlines()[-3],
                      '执行失败' + text)
        self.assertIn(self.user_name, sql_msg.splitlines()[-3], '执行失败' + text)
        self.assertIn(self.view_name_2, sql_msg.splitlines()[-2], '执行失败' + text)
        self.assertIn(self.user_name, sql_msg.splitlines()[-2], '执行失败' + text)

    def tearDown(self):
        text = "-----step7:删除数据库，删除用户，expect:删除成功-----"
        self.logger.info(text)
        drop_cmd = f"drop database {self.db_name_1};" \
            f"drop database {self.db_name_2};" \
            f"drop user {self.user_name} cascade;"
        drop_msg = self.primary_sh.execut_db_sql(drop_cmd)
        self.logger.info(drop_msg)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG,
                      drop_msg, '执行失败' + text)
        self.assertEquals(2, drop_msg.count(
            self.constant.DROP_DATABASE_SUCCESS), '执行失败' + text)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
